package unit16.sax;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.AttributedCharacterIterator.Attribute;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import org.xml.sax.helpers.DefaultHandler;

public class SAXTest {
	public static void main(String[] args) throws Exception {
		String url;
		if(args.length==0){
			url="http://www.w3c.org";
			System.out.println("Using url:"+url);
		}else
			url=args[0];
		
		DefaultHandler handler=new 	DefaultHandler(){
			public void startElement(String namespaceURI,String Lname,String Qname,Attributes attrs){
				if("a".equals(Lname)&&attrs!=null){
					for(int x=0;x<attrs.getLength();x++){
						String aname=attrs.getLocalName(x);
						if("href".equals(aname))
							System.out.println(attrs.getLength());
							System.out.println(attrs.getValue(x));
					}
				}
			}
		};
		
		SAXParserFactory factory=SAXParserFactory.newInstance();
		factory.setNamespaceAware(true);
		factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
		SAXParser saxParser=factory.newSAXParser();
		InputStream in=new URL(url).openStream();
		saxParser.parse(in, handler);
	}
}
